﻿using System;
using System.Collections.Generic;
using System.Text;

namespace CSLADesignerLibrary3
{
    public class CSLADesignerLib3
    {
        private string TurnFirstToUpper(string input)
        {
            string temp = input.Substring(0, 1);
            return temp.ToUpper() + input.Remove(0, 1);
        }
        string fname;
        string ftype;

        #region Code Generation CS
        public string MakeHeaderCS(string ObjectType)
        {
            StringBuilder header = new StringBuilder();
            header.AppendLine("using System;");

            if (ObjectType == "BusinessBase")
            {
                header.AppendLine("using System.Data;");
                header.AppendLine("using System.Data.SqlClient;");
                header.AppendLine("using Csla.Validation;");
            }

            if (ObjectType == "ReadOnlyBase")
            {
                header.AppendLine("using System.Collections.Generic;");
                header.AppendLine("using System.Text;");
                header.AppendLine("using System.Data.SqlClient;");
            }

            if (ObjectType == "ReadOnlyListBase")
            {
                header.AppendLine("using System.Collections.Generic;");
                header.AppendLine("using System.Text;");
                header.AppendLine("using System.Data.SqlClient;");
                header.AppendLine("using System.Data;");
            }

            header.AppendLine("using Csla;");
            header.AppendLine("using Csla.Data;");
            header.AppendLine();
            return header.ToString();
        }

        public string MakeNamespaceStartCS(string NamespaceName)
        {
            StringBuilder NameSpaceStart = new StringBuilder("namespace ");
            NameSpaceStart.AppendLine(NamespaceName);
            NameSpaceStart.AppendLine("{");
            return NameSpaceStart.ToString();
        }

        public string ClassDeclarationCS(string ObjectType, string ObjectName)
        {
            StringBuilder ClassDec = new StringBuilder();

            ClassDec.AppendLine("[Serializable()]");
            ClassDec.Append("public class ");
            if (ObjectType == "BusinessBase")
            {
                ClassDec.Append(ObjectName);
                ClassDec.Append(" : ");
                ClassDec.Append("BusinessBase<");
                ClassDec.Append(ObjectName);
                ClassDec.AppendLine(">");
            }

            if (ObjectType == "ReadOnlyBase")
            {
                ClassDec.Append(ObjectName);
                ClassDec.Append("Info");
                ClassDec.Append(" : ");
                ClassDec.Append("ReadOnlyBase<");
                ClassDec.Append(ObjectName);
                ClassDec.AppendLine("Info>");
            }

            if (ObjectType == "ReadOnlyListBase")
            {
                ClassDec.Append(ObjectName);
                ClassDec.Append("List : ");
                ClassDec.Append("ReadOnlyListBase<");
                ClassDec.Append(ObjectName);
                ClassDec.Append("List, ");
                ClassDec.Append(ObjectName);
                ClassDec.AppendLine("Info>");
            }

            ClassDec.AppendLine("{");



            return ClassDec.ToString();
        }

        #region BusinessBase CS


        #region BusinessBase Business MethodsCS
        //BusinessBase Methods

        public string MakeBusinessMethodsCS(Dictionary<string, string> bbProperties)
        {
            StringBuilder businessmethods = new StringBuilder();
            businessmethods.AppendLine("#region Business Methods");
            int count = 0;
            //string firstname = string.Empty;
            //string firsttype = string.Empty;
            MakeBusinessBaseBusinessMethodsPropertiesDeclarationCS(bbProperties, businessmethods);
            MakeBusinessBaseBusinessMethodsProperitesGetSetCS(bbProperties, businessmethods, count);

            //businessmethods.AppendLine("{");

            //businessmethods.AppendLine("}");

            MakeBusinessBaseBusinessMethodsChildPropertyCS(businessmethods);

            MakeBusinessBaseBusinessMethodsIsValidBoolCS(businessmethods);

            MakeBusinessBaseBusinessMethodsIsDirtyBoolCS(businessmethods);


            MakeBusinessBaseBusinessMethodsGetIdValueCS(businessmethods);

            businessmethods.AppendLine("#endregion");
            return businessmethods.ToString();
        }

        private void MakeBusinessBaseBusinessMethodsProperitesGetSetCS(Dictionary<string, string> bbProperties, StringBuilder businessmethods, int count)
        {
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                if (count == 0)
                {
                    //firsttype = kvp.Value.ToString();
                    //firstname = kvp.Key.ToString();
                    fname = kvp.Key.ToString();
                    ftype = kvp.Value.ToString();
                    businessmethods.AppendLine("[System.ComponentModel.DataObjectField(true, true)]");
                    businessmethods.AppendFormat("public {0} {1}", kvp.Value.ToString(), TurnFirstToUpper(kvp.Key.ToString()));
                    businessmethods.AppendLine();
                    businessmethods.AppendLine("{");
                    businessmethods.AppendLine("[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]");
                    businessmethods.AppendLine("get");
                    businessmethods.AppendLine("{");
                    businessmethods.AppendLine("CanReadProperty(true);");
                    businessmethods.AppendFormat("return _{0};", kvp.Key.ToLower());
                    businessmethods.AppendLine();
                    businessmethods.AppendLine("}");
                    businessmethods.AppendLine("}");
                }
                else
                {
                    if (kvp.Value == "SmartDate")
                    {
                        businessmethods.AppendFormat("public string {0}", TurnFirstToUpper(kvp.Key.ToString()));
                    }
                    else
                    {
                        businessmethods.AppendFormat("public {0} {1}", kvp.Value.ToString(), TurnFirstToUpper(kvp.Key.ToString()));
                    }

                    businessmethods.AppendLine();
                    businessmethods.AppendLine("{");
                    businessmethods.AppendLine("[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]");
                    businessmethods.AppendLine("get");
                    businessmethods.AppendLine("{");
                    businessmethods.AppendLine("CanReadProperty(true);");
                    if (kvp.Value == "SmartDate")
                    {
                        businessmethods.AppendFormat("return _{0}.Text;", kvp.Key.ToLower());
                    }
                    else
                    {
                        businessmethods.AppendFormat("return _{0};", kvp.Key.ToLower());
                    }
                    businessmethods.AppendLine();
                    businessmethods.AppendLine("}");
                    businessmethods.AppendLine("[System.Runtime.CompilerServices.MethodImpl(System.Runtime.CompilerServices.MethodImplOptions.NoInlining)]");
                    businessmethods.AppendLine("set");
                    businessmethods.AppendLine("{");
                    businessmethods.AppendLine("CanWriteProperty(true);");
                    if (kvp.Value == "Int")
                    {
                        businessmethods.AppendLine("if (value == null) value = 0;");
                    }
                    if (kvp.Value == "Guid")
                    {
                        businessmethods.AppendLine("if (value == null) value = Guid.Empty;");
                    }
                    if (kvp.Value == "String")
                    {
                        businessmethods.AppendLine("if (value == null) value = string.Empty;");
                    }

                    businessmethods.AppendFormat("if (_{0} != value)", kvp.Key.ToLower());
                    businessmethods.AppendLine();
                    businessmethods.AppendLine("{");
                    if (kvp.Value == "SmartDate")
                    {
                        businessmethods.AppendFormat("_{0}.Text = value;", kvp.Key.ToLower());
                    }
                    else
                    {
                        businessmethods.AppendFormat("_{0} = value;", kvp.Key.ToLower());
                    }

                    businessmethods.AppendLine();
                    businessmethods.AppendLine("PropertyHasChanged();");
                    businessmethods.AppendLine("}");
                    businessmethods.AppendLine("}");
                    businessmethods.AppendLine("}");




                }

                count = count + 1;
            }
        }
        private static void MakeBusinessBaseBusinessMethodsPropertiesDeclarationCS(Dictionary<string, string> bbProperties, StringBuilder businessmethods)
        {
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                businessmethods.AppendFormat("private {0} _{1};", kvp.Value.ToString(), kvp.Key.ToLower());
                businessmethods.AppendLine();
            }
            businessmethods.AppendLine("private byte[] _timestamp = new byte[8];");
            businessmethods.AppendLine("//use for child object relationships");
            businessmethods.AppendLine("//private ProjectResources _resources;");
        }
        private static void MakeBusinessBaseBusinessMethodsChildPropertyCS(StringBuilder businessmethods)
        {
            businessmethods.AppendLine("//use for child object relationships");
            businessmethods.AppendLine("//public ProjectResources Resources");
            businessmethods.AppendLine("//{");
            businessmethods.AppendLine("//get { return _resources; }");
            businessmethods.AppendLine("//}");
        }
        private static void MakeBusinessBaseBusinessMethodsIsValidBoolCS(StringBuilder businessmethods)
        {
            businessmethods.AppendLine("public override bool IsValid");
            businessmethods.AppendLine("{");
            businessmethods.AppendLine("get { return base.IsValid; }");
            businessmethods.AppendLine("//use for child objects");
            businessmethods.AppendLine("//get { return base.IsValid && _resources.IsValid; }");
            businessmethods.AppendLine("}");
        }
        private static void MakeBusinessBaseBusinessMethodsIsDirtyBoolCS(StringBuilder businessmethods)
        {
            businessmethods.AppendLine("public override bool IsDirty");
            businessmethods.AppendLine("{");
            businessmethods.AppendLine("get { return base.IsDirty; }");
            businessmethods.AppendLine("//get { return base.IsDirty || _resources.IsDirty; }");
            businessmethods.AppendLine("}");
        }
        private void MakeBusinessBaseBusinessMethodsGetIdValueCS(StringBuilder businessmethods)
        {
            businessmethods.AppendLine("protected override object GetIdValue()");
            businessmethods.AppendLine("{");
            businessmethods.AppendFormat("return _{0};", fname.ToLower());
            businessmethods.AppendLine();
            businessmethods.AppendLine("}");
            businessmethods.AppendLine();
        }
        #endregion

        #region BusinessBase ValidationRulesCS
        public string MakeBusinessBaseValidationRulesCS()
        {
            StringBuilder bbValidationRules = new StringBuilder();
            bbValidationRules.AppendLine("#region Validation Rules");

            MakeBusinessBaseValidationRulesSectionCS(bbValidationRules);

            bbValidationRules.AppendLine("#endregion");
            return bbValidationRules.ToString();
        }
    private static void MakeBusinessBaseValidationRulesSectionCS(StringBuilder bbValidationRules)
        {
            bbValidationRules.AppendLine("protected override void AddBusinessRules()");
            bbValidationRules.AppendLine("{");
            bbValidationRules.AppendLine("/* Validation Rules Go Here */");
            bbValidationRules.AppendLine("}");
        }

        #endregion

        #region BusinessBase AuthorizationRulesCS
    
        public string MakeBusinessBaseAuthorizationRulesCS()
        {
            StringBuilder authrules = new StringBuilder();
            authrules.AppendLine("#region Authorization Rules");
            authrules.AppendLine();
            MakeBusinessBaseAuthorizationRulesAddAuthorizationRulesCS(authrules);
            MakeBusinessBaseAuthorizationRulesCanAddObjectCS(authrules);
            MakeBusinessBaseAuthorizationRulesCanGetObjectCS(authrules);
            MakeBusinessBaseAuthorizationRulesCanEditObjectCS(authrules);
            MakeBusinessBaseAuthorizationRulesCanDeleteObjectCS(authrules);
            authrules.AppendLine("#endregion");
            return authrules.ToString();
        }

        private static void MakeBusinessBaseAuthorizationRulesCanDeleteObjectCS(StringBuilder authrules)
        {
            authrules.AppendLine("public static bool CanDeleteObject()");
            authrules.AppendLine("{");
            authrules.AppendLine("return true;");
            authrules.AppendLine("}");
        }
        private static void MakeBusinessBaseAuthorizationRulesCanEditObjectCS(StringBuilder authrules)
        {
            authrules.AppendLine("public static bool CanEditObject()");
            authrules.AppendLine("{");
            authrules.AppendLine("return true;");
            authrules.AppendLine("}");
            authrules.AppendLine();
        }
        private static void MakeBusinessBaseAuthorizationRulesCanGetObjectCS(StringBuilder authrules)
        {
            authrules.AppendLine("public static bool CanGetObject()");
            authrules.AppendLine("{");
            authrules.AppendLine("return true;");
            authrules.AppendLine("}");
            authrules.AppendLine();
        }
        private static void MakeBusinessBaseAuthorizationRulesCanAddObjectCS(StringBuilder authrules)
        {
            authrules.AppendLine("public static bool CanAddObject()");
            authrules.AppendLine("{");
            authrules.AppendLine("return true;");
            authrules.AppendLine("}");
            authrules.AppendLine();
        }
        private static void MakeBusinessBaseAuthorizationRulesAddAuthorizationRulesCS(StringBuilder authrules)
        {
            authrules.AppendLine("protected override void AddAuthorizationRules()");
            authrules.AppendLine("{");
            authrules.AppendLine("/* Add Authorization Rules Here */");
            authrules.AppendLine("}");
            authrules.AppendLine();
        }



        #endregion

        #region BusinessBase FactoryMethodsCS

        public string MakeBusinessBaseFactoryMethodsCS(string ObjectName)
        {
            StringBuilder bbFactoryMethods = new StringBuilder();
            bbFactoryMethods.AppendLine("#region Factory Methods");
            bbFactoryMethods.AppendLine();

            MakeBusinessBaseFactoryMethodsNewObjectCS(ObjectName, bbFactoryMethods);

            MakeBusinessBaseFactoryMethodsGetObjectCS(ObjectName, bbFactoryMethods);

            MakeBusinessBaseFactoryMethodsDeleteObjectCS(ObjectName, bbFactoryMethods);

            MakeBusinessBaseFactoryMethodsPrivateCtorCS(ObjectName, bbFactoryMethods);
            MakeBusinessBaseFactoryMethodsSaveObjectCS(ObjectName, bbFactoryMethods);

            bbFactoryMethods.AppendLine("#endregion");
            return bbFactoryMethods.ToString();
        }
        private static void MakeBusinessBaseFactoryMethodsNewObjectCS(string ObjectName, StringBuilder bbFactoryMethods)
        {
            bbFactoryMethods.AppendFormat("public static {0} New{0}()", ObjectName);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine("{");
            bbFactoryMethods.AppendLine("if(!CanAddObject())");
            bbFactoryMethods.AppendFormat("throw new System.Security.SecurityException(\"User not authorized to add a {0}\");", ObjectName);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendFormat("return DataPortal.Create<{0}>();", ObjectName);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine("}");
        }
        private void MakeBusinessBaseFactoryMethodsGetObjectCS(string ObjectName, StringBuilder bbFactoryMethods)
        {
            bbFactoryMethods.AppendFormat("public static {0} Get{0}({1} {2})", ObjectName, ftype, fname);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine("{");
            bbFactoryMethods.AppendLine("if (!CanGetObject())");
            bbFactoryMethods.AppendFormat("throw new System.Security.SecurityException(\"User not authorized to get a {0}\");", ObjectName);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendFormat("return DataPortal.Fetch<{0}>(new Criteria({1}));", ObjectName, fname);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine("}");
        }
        private void MakeBusinessBaseFactoryMethodsDeleteObjectCS(string ObjectName, StringBuilder bbFactoryMethods)
        {
            bbFactoryMethods.AppendFormat("public static void Delete{0}({1} {2})", ObjectName, ftype, fname);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine("{");
            bbFactoryMethods.AppendLine("if (!CanDeleteObject())");
            bbFactoryMethods.AppendFormat("throw new System.Security.SecurityException(\"User not authorized to delete a {0}\");", ObjectName);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendFormat("DataPortal.Delete(new Criteria({0}));", fname);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine("}");
        }
        private static void MakeBusinessBaseFactoryMethodsPrivateCtorCS(string ObjectName, StringBuilder bbFactoryMethods)
        {
            bbFactoryMethods.AppendFormat("private {0}()", ObjectName);
            bbFactoryMethods.AppendLine("{ }");
        }
        private static void MakeBusinessBaseFactoryMethodsSaveObjectCS(string ObjectName, StringBuilder bbFactoryMethods)
        {
            bbFactoryMethods.AppendFormat("public override {0} Save()", ObjectName);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine("{");
            bbFactoryMethods.AppendLine("if (IsDeleted && !CanDeleteObject())");
            bbFactoryMethods.AppendLine("throw new System.Security.SecurityException(");
            bbFactoryMethods.AppendFormat("\"User not authorized to remove a {0}\");", ObjectName);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine(" else if (IsNew && !CanAddObject())");
            bbFactoryMethods.AppendLine("throw new System.Security.SecurityException(");
            bbFactoryMethods.AppendFormat("\"User not authorized to add a {0}\");", ObjectName);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine(" else if (!CanEditObject())");
            bbFactoryMethods.AppendLine("throw new System.Security.SecurityException(");
            bbFactoryMethods.AppendFormat("\"User not authorized to update a {0}\");", ObjectName);
            bbFactoryMethods.AppendLine();
            bbFactoryMethods.AppendLine("return base.Save();");
            bbFactoryMethods.AppendLine("}");
        }


        #endregion

        #region BusinessBase DataAccessCS

        public string MakeBusinessBaseDataAccessCS(Dictionary<string, string> bbProperties, string DataConnectionName, string BBDataAccessGetSingleStoredProc, string BBAddObjectStoredProc, string BBUpdateStoredProc, string DeleteObjectStoredProc)
        {
            StringBuilder bbDataAccess = new StringBuilder();
            bbDataAccess.AppendLine("#region Data Access");
            bbDataAccess.AppendLine();
            MakeBusinessBaseDataAccessCriteriaClassCS(bbDataAccess);

            MakeBusinessBaseDataAccessDataPortalCreateCS(bbProperties, bbDataAccess);

            MakeBusinessBaseDataAccessDataPortalFetchCS(bbProperties, DataConnectionName, BBDataAccessGetSingleStoredProc, bbDataAccess);
            MakeBusinessBaseDataAccessDataPortalInsertCS(DataConnectionName, BBAddObjectStoredProc, bbDataAccess);

            MakeBusinessBaseDataAccessDataPortalUpdateCS(DataConnectionName, BBUpdateStoredProc, bbDataAccess);
            MakeBusinessBaseDataAccessDoInsertUpdateCS(bbProperties, bbDataAccess);

            MakeBusinessBaseDataAccessDeleteSelfCS(bbDataAccess);

            MakeBusinessBaseDataAccessDataPortalDeleteCS(DataConnectionName, DeleteObjectStoredProc, bbDataAccess);

            bbDataAccess.AppendLine("///protected override void OnDeserialized(System.Runtime.Serialization.StreamingContext context)");
            bbDataAccess.AppendLine("///{");
            bbDataAccess.AppendLine("///_resources.ListChanged += new System.ComponentModel.ListChangedEventHandler(_resources_ListChanged);");
            bbDataAccess.AppendLine("///}");

            bbDataAccess.AppendLine("#endregion");
            return bbDataAccess.ToString();
        }
        private void MakeBusinessBaseDataAccessCriteriaClassCS(StringBuilder bbDataAccess)
        {
            bbDataAccess.AppendLine("[Serializable()]");
            bbDataAccess.AppendLine("private class Criteria");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendFormat("private {0} _{1};", ftype, fname.ToLower());
            bbDataAccess.AppendLine();
            bbDataAccess.AppendFormat("public {0} {1}", ftype, TurnFirstToUpper(fname));
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("{");
            bbDataAccess.Append("get {");
            bbDataAccess.AppendFormat("return _{0};", fname);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendFormat("public Criteria({0} {1})", ftype, TurnFirstToUpper(fname));
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendFormat("_{0}={1};", fname.ToLower(), TurnFirstToUpper(fname));
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("}");

            bbDataAccess.AppendLine("}");
        }
        private static void MakeBusinessBaseDataAccessDataPortalCreateCS(Dictionary<string, string> bbProperties, StringBuilder bbDataAccess)
        {
            bbDataAccess.AppendLine("[RunLocal()]");
            bbDataAccess.AppendLine("protected override void DataPortal_Create()");
            bbDataAccess.AppendLine("{");
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                if (kvp.Value == "SmartDate")
                {
                    bbDataAccess.AppendFormat("_{0}.Date=", kvp.Key.ToLower());
                }
                else
                {
                    bbDataAccess.AppendFormat("_{0}=", kvp.Key.ToLower());
                }
                if (kvp.Value == "Guid")
                    bbDataAccess.AppendLine("Guid.NewGuid();");
                if (kvp.Value == "int")
                    bbDataAccess.AppendLine("0;");
                if (kvp.Value == "string")
                    bbDataAccess.AppendLine("string.Empty;");
                if (kvp.Value == "SmartDate")
                    bbDataAccess.AppendLine("DateTime.Today;");
                bbDataAccess.AppendLine();
            }
            bbDataAccess.AppendLine("ValidationRules.CheckRules();");
            bbDataAccess.AppendLine("}");
        }
        private void MakeBusinessBaseDataAccessDataPortalFetchCS(Dictionary<string, string> bbProperties, string DataConnectionName, string BBDataAccessGetSingleStoredProc, StringBuilder bbDataAccess)
        {
            bbDataAccess.AppendLine("private void DataPortal_Fetch(Criteria criteria)");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendFormat("using (SqlConnection cn = new SqlConnection(Database.{0}))", DataConnectionName);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendLine("cn.Open();");
            bbDataAccess.AppendLine("using (SqlCommand cm = cn.CreateCommand())");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendLine("cm.CommandType = CommandType.StoredProcedure;");
            bbDataAccess.AppendFormat("cm.CommandText = \"{0}\";", BBDataAccessGetSingleStoredProc);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendFormat("cm.Parameters.AddWithValue(\"@firstparam\", criteria.{0});", TurnFirstToUpper(fname));
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendLine("dr.Read();");
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                bbDataAccess.AppendFormat("_{0}=dr.Get", kvp.Key.ToLower());
                if (kvp.Value != "SmartDate")
                {

                    if (kvp.Value == "Guid")
                        bbDataAccess.Append("Guid");
                    if (kvp.Value == "int")
                        bbDataAccess.Append("Int32");
                    if (kvp.Value == "string")
                        bbDataAccess.Append("String");
                    bbDataAccess.AppendFormat("(\"{0}\");", kvp.Key);
                }
                else
                {
                    bbDataAccess.AppendFormat("SmartDate(\"{0}\", _{0}.EmptyIsMin);", kvp.Key);
                }

                bbDataAccess.AppendLine();
            }
            bbDataAccess.AppendLine("///load child objects");
            bbDataAccess.AppendLine("///Following is a sample from ProjectTracker");
            bbDataAccess.AppendLine("///dr.NextResult();");
            bbDataAccess.AppendLine("///_resources.ListChanged -= new System.ComponentModel.ListChangedEventHandler(_resources_ListChanged);");
            bbDataAccess.AppendLine("///_resources = ProjectResources.GetProjectResources(dr);");
            bbDataAccess.AppendLine("///_resources.ListChanged += new System.ComponentModel.ListChangedEventHandler(_resources_ListChanged);");

            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("}");
        }
        private static void MakeBusinessBaseDataAccessDataPortalInsertCS(string DataConnectionName, string BBAddObjectStoredProc, StringBuilder bbDataAccess)
        {
            bbDataAccess.AppendLine("[Transactional(TransactionalTypes.TransactionScope)]");
            bbDataAccess.AppendLine("protected override void DataPortal_Insert()");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendFormat("using (SqlConnection cn = new SqlConnection(Database.{0}))", DataConnectionName);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendLine("cn.Open();");
            bbDataAccess.AppendLine("using (SqlCommand cm = cn.CreateCommand())");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendFormat("cm.CommandText=\"{0}\";", BBAddObjectStoredProc);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("DoInsertUpdate(cm);");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("///update child objects");
            bbDataAccess.AppendLine("//_resources.Update(this);");
            bbDataAccess.AppendLine("}");
        }
        private static void MakeBusinessBaseDataAccessDataPortalUpdateCS(string DataConnectionName, string BBUpdateStoredProc, StringBuilder bbDataAccess)
        {
            bbDataAccess.AppendLine("[Transactional(TransactionalTypes.TransactionScope)]");
            bbDataAccess.AppendLine("protected override void DataPortal_Update()");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendLine("if (base.IsDirty)");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendFormat("using (SqlConnection cn = new SqlConnection(Database.{0}))", DataConnectionName);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendLine("cn.Open();");
            bbDataAccess.AppendLine("using (SqlCommand cm = cn.CreateCommand())");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendFormat("cm.CommandText=\"{0}\";", BBUpdateStoredProc);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("cm.Parameters.AddWithValue(\"@lastChanged\", _timestamp);");
            bbDataAccess.AppendLine("DoInsertUpdate(cm);");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("///update child objects");
            bbDataAccess.AppendLine("//_resources.Update(this);");
            bbDataAccess.AppendLine("}");
        }
        private static void MakeBusinessBaseDataAccessDoInsertUpdateCS(Dictionary<string, string> bbProperties, StringBuilder bbDataAccess)
        {
            bbDataAccess.AppendLine("private void DoInsertUpdate(SqlCommand cm)");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendLine("cm.CommandType = CommandType.StoredProcedure;");
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                bbDataAccess.AppendFormat("cm.Parameters.AddWithValue(\"{0}\", _{1}", kvp.Key, kvp.Key.ToLower());
                if (kvp.Value == "SmartDate")
                    bbDataAccess.Append(".DBValue");
                bbDataAccess.AppendLine(");");

            }
            bbDataAccess.AppendLine("SqlParameter param = new SqlParameter(\"@newLastChanged\", SqlDbType.Timestamp);");
            bbDataAccess.AppendLine("param.Direction = ParameterDirection.Output;");
            bbDataAccess.AppendLine("cm.Parameters.Add(param);");
            bbDataAccess.AppendLine("cm.ExecuteNonQuery();");
            bbDataAccess.AppendLine("_timestamp = (byte[])cm.Parameters[\"@newLastChanged\"].Value;");
            bbDataAccess.AppendLine("}");
        }
        private void MakeBusinessBaseDataAccessDeleteSelfCS(StringBuilder bbDataAccess)
        {
            bbDataAccess.AppendLine("[Transactional(TransactionalTypes.TransactionScope)]");
            bbDataAccess.AppendLine("protected override void DataPortal_DeleteSelf()");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendFormat("DataPortal_Delete(new Criteria(_{0}));", fname);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("}");
        }
        private void MakeBusinessBaseDataAccessDataPortalDeleteCS(string DataConnectionName, string DeleteObjectStoredProc, StringBuilder bbDataAccess)
        {
            bbDataAccess.AppendLine("[Transactional(TransactionalTypes.TransactionScope)]");
            bbDataAccess.AppendLine("private void DataPortal_Delete(Criteria criteria)");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendFormat("using (SqlConnection cn = new SqlConnection(Database.{0}))", DataConnectionName);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendLine("cn.Open();");
            bbDataAccess.AppendLine("using (SqlCommand cm = cn.CreateCommand())");
            bbDataAccess.AppendLine("{");
            bbDataAccess.AppendLine("cm.CommandType = CommandType.StoredProcedure;");
            bbDataAccess.AppendFormat("cm.CommandText = \"{0}\";", DeleteObjectStoredProc);
            bbDataAccess.AppendLine();
            bbDataAccess.AppendFormat("cm.Parameters.AddWithValue(\"@{0}\", criteria.{0});", TurnFirstToUpper(fname));
            bbDataAccess.AppendLine();
            bbDataAccess.AppendLine("cm.ExecuteNonQuery();");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("}");
            bbDataAccess.AppendLine("}");
        }
        #endregion

        #region BusinessBase ExistsCS


        public string MakeBusinessBaseBExistsCS(string ObjectName, string DataConnectionName, string ObjectExistsStoredProc)
        {
            StringBuilder bbExists = new StringBuilder();
            bbExists.AppendLine("#region Exists");

            MakeBusinessBaseBExistsBoolCS(bbExists);

            bbExists.AppendLine("[Serializable()]");
            bbExists.AppendLine("private class ExistsCommand : CommandBase");
            bbExists.AppendLine("{");
            bbExists.AppendFormat("private {0} _{1};", ftype, fname);
            bbExists.AppendLine();
            bbExists.AppendLine("private bool _exists;");
            bbExists.AppendLine();

            MakeBusinessBaseBExistsBoolObjectExistsCS(ObjectName, bbExists);

            MakeBusinessBaseBExistsBoolExistsCS(ObjectName, bbExists);

            MakeBusinessBaseBExistsCommandCS(bbExists);

            MakeBusinessBaseBExistsDataPortalExecuteCS(DataConnectionName, ObjectExistsStoredProc, bbExists);
            bbExists.AppendLine("}");


            bbExists.AppendLine("#endregion");

            return bbExists.ToString();
        }
        private static void MakeBusinessBaseBExistsBoolObjectExistsCS(string ObjectName, StringBuilder bbExists)
        {
            bbExists.AppendFormat("public bool {0}Exists", ObjectName);
            bbExists.AppendLine("{");
            bbExists.AppendLine("get { return _exists; }");
            bbExists.AppendLine("}");
            bbExists.AppendLine();
        }
        private void MakeBusinessBaseBExistsCommandCS(StringBuilder bbExists)
        {
            bbExists.AppendFormat("private ExistsCommand({0} {1})", ftype, fname);
            bbExists.AppendLine();
            bbExists.AppendLine("{");
            bbExists.AppendFormat("_{0} = {0};", fname);
            bbExists.AppendLine("}");
            bbExists.AppendLine();
        }
        private void MakeBusinessBaseBExistsBoolExistsCS(string ObjectName, StringBuilder bbExists)
        {
            bbExists.AppendFormat("public static bool Exists({0} {1})", ftype, fname);
            bbExists.AppendLine();
            bbExists.AppendLine("{");
            bbExists.AppendLine("ExistsCommand result;");
            bbExists.AppendFormat("result = DataPortal.Execute<ExistsCommand>(new ExistsCommand({0}));", fname);
            bbExists.AppendLine();
            bbExists.AppendFormat("return result.{0}Exists;", ObjectName);
            bbExists.AppendLine();
            bbExists.AppendLine("}");
            bbExists.AppendLine();
        }
        private void MakeBusinessBaseBExistsDataPortalExecuteCS(string DataConnectionName, string ObjectExistsStoredProc, StringBuilder bbExists)
        {
            bbExists.AppendLine("protected override void DataPortal_Execute()");
            bbExists.AppendLine("{");
            bbExists.AppendFormat("using (SqlConnection cn = new SqlConnection(Database.{0}))", DataConnectionName);
            bbExists.AppendLine();
            bbExists.AppendLine("{");
            bbExists.AppendLine("cn.Open();");
            bbExists.AppendLine("using (SqlCommand cm = cn.CreateCommand())");
            bbExists.AppendLine("{");
            bbExists.AppendLine("cm.CommandType = CommandType.StoredProcedure;");
            bbExists.AppendFormat("cm.CommandText = \"{0}\";", ObjectExistsStoredProc);
            bbExists.AppendLine();
            bbExists.AppendFormat("cm.Parameters.AddWithValue(\"@{0}\", _{0});", fname);
            bbExists.AppendLine();
            bbExists.AppendLine("int count = (int)cm.ExecuteScalar();");
            bbExists.AppendLine("_exists = (count > 0);");
            bbExists.AppendLine("}");
            bbExists.AppendLine("}");
            bbExists.AppendLine("}");
        }
        private void MakeBusinessBaseBExistsBoolCS(StringBuilder bbExists)
        {
            bbExists.AppendFormat("public static bool Exists({0} {1})", ftype, fname);
            bbExists.AppendLine();
            bbExists.AppendLine("{");
            bbExists.AppendFormat("return ExistsCommand.Exists({0});", fname);
            bbExists.AppendLine();
            bbExists.AppendLine("}");
            bbExists.AppendLine();
        }
        #endregion
        #endregion



        #region ReadOnlyListBase FactoryMethods CS


        public string MakeReadOnlyListBaseFactoryMethodsCS(string ObjectName, string FilteredCriteriaType, string FilteredCriteriaName)
        {
            StringBuilder factorymethods = new StringBuilder();
            factorymethods.AppendLine("#region Factory Methods");

            MakeReadOnlyListBaseFactoryMethodsGetAllCS(ObjectName, factorymethods);

            MakeReadOnlyListBaseFactoryMethodsGetFilteredCS(ObjectName, FilteredCriteriaType, FilteredCriteriaName, factorymethods);

            MakeReadOnlyListBaseForceFactoryMethodCS(ObjectName, factorymethods);

            factorymethods.AppendLine("#endregion");

            return factorymethods.ToString();
        }

        private static void MakeReadOnlyListBaseFactoryMethodsGetAllCS(string ObjectName, StringBuilder factorymethods)
        {
            //Get All
            //start method comments
            factorymethods.AppendLine("///<summary>");
            factorymethods.Append("///Return a list of all ");
            factorymethods.Append(ObjectName);
            factorymethods.AppendLine("s");
            factorymethods.AppendLine("///</summary>");
            //end method comments
            //start method
            factorymethods.Append("public static ");
            factorymethods.Append(ObjectName);
            factorymethods.Append("List Get");
            factorymethods.Append(ObjectName);
            factorymethods.AppendLine("()");
            factorymethods.AppendLine("{");
            factorymethods.Append("return DataPortal.Fetch<");
            factorymethods.Append(ObjectName);
            factorymethods.AppendLine("List>(new Criteria());");
            factorymethods.AppendLine("}");
            //end Get ALL
        }
        private static void MakeReadOnlyListBaseFactoryMethodsGetFilteredCS(string ObjectName, string FilteredCriteriaType, string FilteredCriteriaName, StringBuilder factorymethods)
        {
            //Get Filtered
            //start Method Comments
            factorymethods.AppendLine("///<summary>");
            factorymethods.Append("///Return a list of ");

            factorymethods.Append(ObjectName);
            factorymethods.AppendLine("s");
            factorymethods.AppendLine("/// filtered by project name. ");
            factorymethods.AppendLine("///</summary>");
            //end method comments
            //startmethod
            factorymethods.Append("public static ");
            factorymethods.Append(ObjectName);
            factorymethods.Append("List Get");
            factorymethods.Append(ObjectName);
            factorymethods.AppendFormat("List({0} {1})", FilteredCriteriaType, FilteredCriteriaName);
            factorymethods.AppendLine();
            factorymethods.AppendLine("{");
            factorymethods.Append("return DataPortal.Fetch<");
            factorymethods.Append(ObjectName);
            factorymethods.AppendFormat("List>(new FilteredCriteria({0}));", FilteredCriteriaName);
            factorymethods.AppendLine();
            factorymethods.AppendLine("}");
            //end method
        }
        private static void MakeReadOnlyListBaseForceFactoryMethodCS(string ObjectName, StringBuilder factorymethods)
        {
            //force factory method
            factorymethods.Append("private ");
            factorymethods.Append(ObjectName);
            factorymethods.AppendLine("List()");
            factorymethods.AppendLine("{/* require use of factory methods */}");
        }

        public string MakeReadOnlyListBaseDataAccessCS(string ObjectName, int FilteredCriteriaTypeIndex, string FilteredCriteriaType, string FilteredCriteriaName, string DataConnectionName, string ROLBSP)
        {
            StringBuilder rolbDataAccess = new StringBuilder();
            rolbDataAccess.AppendLine("#region Data Access");
            rolbDataAccess.AppendLine();

            rolbDataAccess.AppendLine("[Serializable()]");
            rolbDataAccess.AppendLine("private class Criteria");
            rolbDataAccess.AppendLine("{ /* No Criteria Select All */ }");
            rolbDataAccess.AppendLine();
            rolbDataAccess.AppendLine("[Serializable()]");
            rolbDataAccess.AppendLine("private class FilteredCriteria");
            rolbDataAccess.AppendLine("{");
            if (FilteredCriteriaTypeIndex == -1)
            {
                rolbDataAccess.AppendLine("private string _name");
                rolbDataAccess.AppendLine("public string Name");
                rolbDataAccess.AppendLine("{");
                rolbDataAccess.AppendLine("get { return _name;}");
                rolbDataAccess.AppendLine("public FilteredCriteria(string name)");
                rolbDataAccess.AppendLine("{");
                rolbDataAccess.AppendLine("_name=name;");
                rolbDataAccess.AppendLine("}");
            }
            else
            {
                switch (FilteredCriteriaType)
                {
                    case "int":
                        rolbDataAccess.Append("private int _");
                        break;
                    case "string":
                        rolbDataAccess.Append("private string _");
                        break;
                    case "Guid":
                        rolbDataAccess.Append("private Guid _");
                        break;
                    default:
                        break;

                }
                rolbDataAccess.Append(FilteredCriteriaName.ToLower());
                rolbDataAccess.AppendLine(";");
                switch (FilteredCriteriaType)
                {
                    case "int":
                        rolbDataAccess.Append("public int ");
                        break;
                    case "string":
                        rolbDataAccess.Append("public string ");
                        break;
                    case "Guid":
                        rolbDataAccess.Append("public Guid ");
                        break;
                    default:
                        break;

                }

                rolbDataAccess.AppendLine(FilteredCriteriaName);
                rolbDataAccess.AppendLine("{");
                rolbDataAccess.Append("get { return _");
                rolbDataAccess.Append(FilteredCriteriaName.ToLower());
                rolbDataAccess.AppendLine(";}");
                rolbDataAccess.AppendLine("}");
                rolbDataAccess.Append("public FilteredCriteria(");
                rolbDataAccess.Append(FilteredCriteriaType);//todo: fix this

                rolbDataAccess.Append(" ");
                rolbDataAccess.Append(FilteredCriteriaName.ToLower());
                rolbDataAccess.AppendLine(")");
                rolbDataAccess.AppendLine("{");
                rolbDataAccess.Append("_");
                rolbDataAccess.Append(FilteredCriteriaName.ToLower());
                rolbDataAccess.Append("=");
                rolbDataAccess.Append(FilteredCriteriaName.ToLower());
                rolbDataAccess.AppendLine(";");
                rolbDataAccess.AppendLine("}");
                rolbDataAccess.AppendLine("}");



            }

            //Fetch No Criteria
            rolbDataAccess.AppendLine("private void DataPortal_Fetch(Criteria criteria)");
            rolbDataAccess.AppendLine("{");
            rolbDataAccess.AppendLine("//Fetch with no filter.");
            rolbDataAccess.Append("Fetch(");
            rolbDataAccess.Append("\"\"");
            rolbDataAccess.AppendLine(");");
            rolbDataAccess.AppendLine("}");

            //Fetch Criteria
            rolbDataAccess.AppendLine("private void DataPortal_Fetch(FilteredCriteria criteria)");
            rolbDataAccess.AppendLine("{");
            rolbDataAccess.AppendLine("///Fetch with filter.");
            rolbDataAccess.Append("Fetch(");
            rolbDataAccess.Append("criteria.");
            rolbDataAccess.Append(FilteredCriteriaName);
            rolbDataAccess.AppendLine(");");
            rolbDataAccess.AppendLine("}");

            //the Fetch        
            rolbDataAccess.Append("private void Fetch(string ");
            rolbDataAccess.Append(FilteredCriteriaName.ToLower());
            rolbDataAccess.AppendLine("Filter)");
            rolbDataAccess.AppendLine("{");
            rolbDataAccess.AppendLine("this.RaiseListChangedEvents = false;");
            rolbDataAccess.Append("using (SqlConnection cn = new SqlConnection(Database.");
            rolbDataAccess.Append(DataConnectionName);
            rolbDataAccess.AppendLine("))");
            rolbDataAccess.AppendLine("{");
            rolbDataAccess.AppendLine("cn.Open();");
            rolbDataAccess.AppendLine("using (SqlCommand cm = cn.CreateCommand())");
            rolbDataAccess.AppendLine("{");
            rolbDataAccess.AppendLine("cm.CommandType = CommandType.StoredProcedure;");
            rolbDataAccess.Append("cm.CommandText = \"");
            rolbDataAccess.Append(ROLBSP);
            rolbDataAccess.AppendLine("\";");
            rolbDataAccess.AppendLine("using (SafeDataReader dr = new SafeDataReader(cm.ExecuteReader()))");
            rolbDataAccess.AppendLine("{");
            rolbDataAccess.AppendLine("IsReadOnly = false;");
            rolbDataAccess.AppendLine("while (dr.Read())");
            rolbDataAccess.AppendLine("{");
            rolbDataAccess.AppendLine("ProjectInfo info = new ProjectInfo(");
            rolbDataAccess.AppendLine("dr.GetGuid(0),");
            rolbDataAccess.AppendLine("dr.GetString(1));");
            rolbDataAccess.AppendLine(" /// apply filter if necessary");
            rolbDataAccess.Append("  if ((");
            rolbDataAccess.Append(FilteredCriteriaName.ToLower());
            rolbDataAccess.Append("Filter.Length == 0) || (info.");
            rolbDataAccess.Append(FilteredCriteriaName);
            rolbDataAccess.AppendLine(".IndexOf(nameFilter) == 0))");
            rolbDataAccess.AppendLine("    this.Add(info);");
            rolbDataAccess.AppendLine("}");
            rolbDataAccess.AppendLine("IsReadOnly = true;");
            rolbDataAccess.AppendLine("}");
            rolbDataAccess.AppendLine("}");
            rolbDataAccess.AppendLine("}");
            rolbDataAccess.AppendLine("this.RaiseListChangedEvents = true;");
            rolbDataAccess.AppendLine("}");




            rolbDataAccess.AppendLine("#endregion");
            return rolbDataAccess.ToString();
        }


        #endregion






        #region ReadOnly base
        #region BusinessBase


        public string MakeReadOnlyBusinessBaseCS(Dictionary<string, string> roBusinessBaseProperties)
        {
            StringBuilder roBusinessBase = new StringBuilder();
            roBusinessBase.AppendLine("#region Business Methods");

            MakeROPropertiesCS(roBusinessBaseProperties, roBusinessBase);
            MakeROGetIDValueCS(roBusinessBase);
            MakeROToStringCS(roBusinessBase);
            roBusinessBase.AppendLine("#endregion");
            return roBusinessBase.ToString();
        }



        private void MakeROPropertiesCS(Dictionary<string, string> roBusinessBaseProperties, StringBuilder roBusinessBase)
        {
            //properties
            int count = 0;

            foreach (KeyValuePair<string, string> kvp in roBusinessBaseProperties)
            {
                roBusinessBase.AppendFormat("private {0} _{1};", kvp.Value.ToString(), kvp.Key.ToLower());
                roBusinessBase.AppendLine();

            }

            foreach (KeyValuePair<string, string> kvp in roBusinessBaseProperties)
            {
                string type = kvp.Value.ToString();
                string name = kvp.Key.ToString();

                if (count == 0)
                {
                    ftype = kvp.Value.ToString();
                    fname = kvp.Key.ToString();
                }
                roBusinessBase.AppendFormat("public {0} {1}", type, TurnFirstToUpper(name));
                roBusinessBase.AppendLine();
                roBusinessBase.AppendLine("{");
                roBusinessBase.AppendFormat(" get {0} return _{1};{2}", "{", name.ToLower(), "}");
                roBusinessBase.AppendLine();
                roBusinessBase.AppendLine("}");
                count = count + 1;
            }
        }
        private void MakeROGetIDValueCS(StringBuilder roBusinessBase)
        {
            roBusinessBase.AppendLine("protected override object GetIdValue()");
            roBusinessBase.AppendLine("{");

            roBusinessBase.AppendFormat("return _{0};", fname.ToLower());
            roBusinessBase.AppendLine();
            roBusinessBase.AppendLine("}");
            roBusinessBase.AppendLine();
        }
        private void MakeROToStringCS(StringBuilder roBusinessBase)
        {
            roBusinessBase.AppendLine("public override string ToString()");
            roBusinessBase.AppendLine("{");
            roBusinessBase.AppendFormat("return _{0}.ToString();", fname.ToLower());
            roBusinessBase.AppendLine();
            roBusinessBase.AppendLine("}");
            roBusinessBase.AppendLine();
        }
        #endregion

        #region Constructors

        public string MakeReadOnlyConstructorsCS(string ObjectName, Dictionary<string, string> roBusinessBaseProperties)
        {
            StringBuilder roConstructors = new StringBuilder();

            roConstructors.AppendLine("#region Constructors");

            MakeROctorCS(ObjectName, roConstructors);

            
            MakeROctor2CS(roBusinessBaseProperties, roConstructors, ObjectName);


            roConstructors.AppendLine("#endregion");
            return roConstructors.ToString();
        }

        private static void MakeROctor2CS(Dictionary<string, string> roBusinessBaseProperties, StringBuilder roConstructors, string ObjectName)
        {
            roConstructors.AppendFormat("internal {0}Info(", ObjectName);
            foreach (KeyValuePair<string, string> kvp in roBusinessBaseProperties)
            {
                roConstructors.AppendFormat("{0} {1},", kvp.Value.ToString(), kvp.Key.ToString());
            }
            roConstructors.Remove((roConstructors.Length - 1), 1);
            roConstructors.AppendLine(")");
            roConstructors.AppendLine("{");
            foreach (KeyValuePair<string, string> kvp in roBusinessBaseProperties)
            {
                roConstructors.AppendFormat("_{0}={1};", kvp.Key.ToLower(), kvp.Key.ToString());
                roConstructors.AppendLine();
            }
            roConstructors.AppendLine("}");
            roConstructors.AppendLine();
        }
        private static void MakeROctorCS(string ObjectName, StringBuilder roConstructors)
        {
            roConstructors.AppendFormat("private {0}Info()", ObjectName);
            roConstructors.AppendLine();
            roConstructors.AppendLine("{ /* require use of Factory Methods */ }");
            roConstructors.AppendLine();
        }
        #endregion

        #endregion

       

        public string MakeCloseClassCS()
        {
            StringBuilder classclose = new StringBuilder();
            classclose.AppendLine("}");
            classclose.AppendLine("}");

            return classclose.ToString();
        }

        #endregion

        #region Code Generation VB

        private string ConvertCtoVBTypes(string ctype)
        {
            string outstring = string.Empty;
            if (ctype == "int")
            {
                outstring = "Integer";
            }
            else
            {
                outstring = ctype;
            }

            return outstring;
        }

        public string ClassDeclarationVB(string ObjectType, string ObjectName)
        {
            StringBuilder classDecVB = new StringBuilder();
            classDecVB.AppendLine("<Serializable()> _");

            if (ObjectType == "BusinessBase")
            {
                classDecVB.AppendFormat("Public Class {0}", ObjectName);
                classDecVB.AppendLine();
                classDecVB.AppendFormat("Inherits BusinessBase(Of {0})", ObjectName);
                classDecVB.AppendLine();
            }

            if (ObjectType == "ReadOnlyBase")
            {
                classDecVB.AppendFormat("Public Class {0}Info", ObjectName);
                classDecVB.AppendLine();
                classDecVB.AppendFormat("Inherits ReadOnlyBase(Of {0}Info)", ObjectName);
                classDecVB.AppendLine();
            }

            if (ObjectType == "ReadOnlyListBase")
            {
                classDecVB.AppendFormat("Public Class {0}List", ObjectName);
                classDecVB.AppendLine();
                classDecVB.AppendFormat("Inherits ReadOnlyListBase(Of {0}List, {0}Info)", ObjectName);
                classDecVB.AppendLine();
            }

            return classDecVB.ToString();
        }

        #region BusinessBase


        #region Business Methods


        public string MakeBusinessMethodsVB(Dictionary<string, string> bbProperties)
        {
            StringBuilder vbBusinessMethods = new StringBuilder();
            vbBusinessMethods.AppendLine();
            vbBusinessMethods.AppendLine("#Region \"Business Methods\"");

            int count = 0;
            MakeBusinessBaseBusinessMethodsPropertiesDeclarationVB(bbProperties, vbBusinessMethods);
            MakeBusinessBaseBusinessMethodsProperitesGetSetVB(bbProperties, vbBusinessMethods, count);
            MakeBusinessBaseBusinessMethodsChildPropertyVB(vbBusinessMethods);

            MakeBusinessBaseBusinessMethodsIsValidBoolVB(vbBusinessMethods);

            MakeBusinessBaseBusinessMethodsIsDirtyBoolVB(vbBusinessMethods);

            MakeBusinessBaseBusinessMethodsGetIdValueVB(vbBusinessMethods);

            vbBusinessMethods.AppendLine("#End Region");
            return vbBusinessMethods.ToString();
        }

        private void MakeBusinessBaseBusinessMethodsPropertiesDeclarationVB(Dictionary<string, string> bbProperties, StringBuilder vbBusinessMethods)
        {
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                vbBusinessMethods.AppendFormat("Private m{0} as {1} ", TurnFirstToUpper(kvp.Key), ConvertCtoVBTypes(kvp.Value));
                if (kvp.Value == "Guid")
                {
                    vbBusinessMethods.Append("=Guid.NewGuid()");
                }
                if (kvp.Value == "string")
                {
                    vbBusinessMethods.Append("=\"\"");
                }
                if (kvp.Value == "Int")
                {
                    vbBusinessMethods.Append("=0");
                }
                if (kvp.Value == "SmartDate")
                {
                    vbBusinessMethods.Append("= new SmartDate");

                }
                vbBusinessMethods.AppendLine();
            }

            vbBusinessMethods.AppendLine("Private mTimestamp(7) As Byte");
            vbBusinessMethods.AppendLine("' use for child object relationships");
            vbBusinessMethods.AppendLine("'Private mResources As ProjectResources = ProjectResources.NewProjectResources()");
            vbBusinessMethods.AppendLine();
        }


        private void MakeBusinessBaseBusinessMethodsProperitesGetSetVB(Dictionary<string, string> bbProperties, StringBuilder vbBusinessMethods, int count)
        {
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                if (count == 0)
                {
                    fname = kvp.Key.ToString();
                    ftype = kvp.Value.ToString();
                    vbBusinessMethods.AppendLine("<System.ComponentModel.DataObjectField(True, True)> _");
                    vbBusinessMethods.AppendFormat("Public ReadOnly Property {0}() As {1}", TurnFirstToUpper(fname), ConvertCtoVBTypes(ftype));
                    vbBusinessMethods.AppendLine();
                    vbBusinessMethods.AppendLine("<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _");
                    vbBusinessMethods.AppendLine("Get");
                    vbBusinessMethods.AppendLine("CanReadProperty(True)");
                    vbBusinessMethods.AppendFormat("Return m{0}", fname);
                    vbBusinessMethods.AppendLine();
                    vbBusinessMethods.AppendLine("End Get");
                    vbBusinessMethods.AppendLine("End Property");

                }
                else
                {
                    if (kvp.Value == "SmartDate")
                    {
                        vbBusinessMethods.AppendFormat("Public Property {0}() As String", TurnFirstToUpper(kvp.Key));
                    }
                    else
                    {
                        vbBusinessMethods.AppendFormat("Public Property {0}() As {1}", TurnFirstToUpper(kvp.Key), ConvertCtoVBTypes(kvp.Value));
                    }

                    vbBusinessMethods.AppendLine();
                    vbBusinessMethods.AppendLine("<System.Runtime.CompilerServices.MethodImpl(Runtime.CompilerServices.MethodImplOptions.NoInlining)> _");
                    vbBusinessMethods.AppendLine("Get");
                    vbBusinessMethods.AppendLine("CanReadProperty(True)");
                    if (kvp.Value == "SmartDate")
                    {
                        vbBusinessMethods.AppendFormat("Return m{0}.Text", TurnFirstToUpper(kvp.Key));
                    }
                    else
                    {
                        vbBusinessMethods.AppendFormat("Return m{0}", TurnFirstToUpper(kvp.Key));
                    }
                    vbBusinessMethods.AppendLine();
                    vbBusinessMethods.AppendLine("End Get");
                    if (kvp.Value == "SmartDate")
                    {
                        vbBusinessMethods.AppendFormat("Set(Byval value As String)", kvp.Value);
                    }
                    else
                    {
                        vbBusinessMethods.AppendFormat("Set(Byval value As {0})", kvp.Value);
                    }
                    vbBusinessMethods.AppendLine();
                    vbBusinessMethods.AppendLine("CanWriteProperty(True)");
                    vbBusinessMethods.Append("If Value Is Nothing Then Value = ");
                    if (kvp.Value == "Guid")
                    {
                        vbBusinessMethods.AppendLine("Guid.Empty");

                    }
                    else
                    {
                        vbBusinessMethods.AppendLine("\"\"");

                    }
                    vbBusinessMethods.AppendFormat("If m{0} <> Value Then", kvp.Key);
                    vbBusinessMethods.AppendLine();
                    if (kvp.Value == "SmartDate")
                    {
                        vbBusinessMethods.AppendFormat("m{0}.Text = Value", kvp.Key);
                    }
                    else
                    {
                        vbBusinessMethods.AppendFormat("m{0} = Value", kvp.Key);
                    }
                    vbBusinessMethods.AppendLine();
                    vbBusinessMethods.AppendLine("PropertyHasChanged()");
                    vbBusinessMethods.AppendLine("End If");
                    vbBusinessMethods.AppendLine("End Set");
                    vbBusinessMethods.AppendLine("End Property");


                }

                count = count + 1;
            }
        }


        private static void MakeBusinessBaseBusinessMethodsChildPropertyVB(StringBuilder vbBusinessMethods)
        {
            //Child Objects
            vbBusinessMethods.AppendLine("'For use with child objects");
            vbBusinessMethods.AppendLine("'Public ReadOnly Property Resources() As ProjectResources");
            vbBusinessMethods.AppendLine("'Get");
            vbBusinessMethods.AppendLine("'Return mResources");
            vbBusinessMethods.AppendLine("'End Get");
            vbBusinessMethods.AppendLine("'End Property");
            vbBusinessMethods.AppendLine();
        }


        private static void MakeBusinessBaseBusinessMethodsIsValidBoolVB(StringBuilder vbBusinessMethods)
        {
            //IsValid
            vbBusinessMethods.AppendLine("Public Overrides ReadOnly Property IsValid() As Boolean");
            vbBusinessMethods.AppendLine("Get");
            vbBusinessMethods.AppendLine("Return MyBase.IsValid 'AndAlso mResources.IsValid  --if using child objects");
            vbBusinessMethods.AppendLine("End Get");
            vbBusinessMethods.AppendLine("End Property");
            vbBusinessMethods.AppendLine();
        }


        private static void MakeBusinessBaseBusinessMethodsIsDirtyBoolVB(StringBuilder vbBusinessMethods)
        {
            //IsDirty
            vbBusinessMethods.AppendLine("Public Overrides ReadOnly Property IsDirty() As Boolean");
            vbBusinessMethods.AppendLine("Get");
            vbBusinessMethods.AppendLine("Return MyBase.IsDirty 'AndAlso mResources.IsDirty  --if using child objects");
            vbBusinessMethods.AppendLine("End Get");
            vbBusinessMethods.AppendLine("End Property");
            vbBusinessMethods.AppendLine();
        }


        private void MakeBusinessBaseBusinessMethodsGetIdValueVB(StringBuilder vbBusinessMethods)
        {
            //IDValue
            vbBusinessMethods.AppendLine("Protected Overrides Function GetIdValue() as Object");
            vbBusinessMethods.AppendFormat("Return m{0}", TurnFirstToUpper(fname));
            vbBusinessMethods.AppendLine();
            vbBusinessMethods.AppendLine("End Function");
        }
        #endregion


        #region Validation Methods

        public string MakeValidationRulesVB()
        {
            StringBuilder vbValidationRules = new StringBuilder();
            vbValidationRules.AppendLine("#Region \"Validation Rules\"");

            MakeBusinessBaseValidationRulesSectionVB(vbValidationRules);

            vbValidationRules.AppendLine("#End Region");

            return vbValidationRules.ToString();
        }

        private static void MakeBusinessBaseValidationRulesSectionVB(StringBuilder vbValidationRules)
        {
            vbValidationRules.AppendLine("Protected Overrides Sub AddBusinessRules()");
            vbValidationRules.AppendLine("' add validation rules here");
            vbValidationRules.AppendLine("End Sub");
        }

        #endregion

        #region Authorization Methods

        public string MakeAuthorizationRulesVB()
        {
            StringBuilder vbAuthorizationRules = new StringBuilder();
            vbAuthorizationRules.AppendLine("#Region \"Authorization Rules\"");
            vbAuthorizationRules.AppendLine();

            MakeBusinessBaseAuthorizationRulesAddAuthorizationRulesVB(vbAuthorizationRules);

            MakeBusinessBaseAuthorizationRulesCanAddObjectVB(vbAuthorizationRules);

            MakeBusinessBaseAuthorizationRulesCanGetObjectVB(vbAuthorizationRules);

            MakeBusinessBaseAuthorizationRulesCanDeleteObjectVB(vbAuthorizationRules);

            MakeBusinessBaseAuthorizationRulesCanEditObjectVB(vbAuthorizationRules);

            vbAuthorizationRules.AppendLine("#End Region");

            return vbAuthorizationRules.ToString();
        }

        private static void MakeBusinessBaseAuthorizationRulesAddAuthorizationRulesVB(StringBuilder vbAuthorizationRules)
        {
            //AddAuthorizationRules
            vbAuthorizationRules.AppendLine("Protected Overrides Sub AddAuthorizationRules()");
            vbAuthorizationRules.AppendLine("'Add authorization rules");
            vbAuthorizationRules.AppendLine("End Sub");
            vbAuthorizationRules.AppendLine();
        }
        private static void MakeBusinessBaseAuthorizationRulesCanAddObjectVB(StringBuilder vbAuthorizationRules)
        {
            //Object Rules
            vbAuthorizationRules.AppendLine("Public Shared Function CanAddObject() As Boolean");
            vbAuthorizationRules.AppendLine("Return True");
            vbAuthorizationRules.AppendLine("End Function");
            vbAuthorizationRules.AppendLine();
        }
        private static void MakeBusinessBaseAuthorizationRulesCanGetObjectVB(StringBuilder vbAuthorizationRules)
        {
            vbAuthorizationRules.AppendLine("Public Shared Function CanGetObject() As Boolean");
            vbAuthorizationRules.AppendLine("Return True");
            vbAuthorizationRules.AppendLine("End Function");
            vbAuthorizationRules.AppendLine();
        }
        private static void MakeBusinessBaseAuthorizationRulesCanDeleteObjectVB(StringBuilder vbAuthorizationRules)
        {
            vbAuthorizationRules.AppendLine("Public Shared Function CanDeleteObject() As Boolean");
            vbAuthorizationRules.AppendLine("Return True");
            vbAuthorizationRules.AppendLine("End Function");
            vbAuthorizationRules.AppendLine();
        }
        private static void MakeBusinessBaseAuthorizationRulesCanEditObjectVB(StringBuilder vbAuthorizationRules)
        {
            vbAuthorizationRules.AppendLine("Public Shared Function CanEditObject() As Boolean");
            vbAuthorizationRules.AppendLine("Return True");
            vbAuthorizationRules.AppendLine("End Function");
            vbAuthorizationRules.AppendLine();
        }
        #endregion

        #region Factory Methods


        public string MakeBBFactoryMethodsVB(string ObjectName)
        {
            StringBuilder vbBBfactoryMethods = new StringBuilder();
            vbBBfactoryMethods.AppendLine("#Region \"Factory Methods\"");
            vbBBfactoryMethods.AppendLine();

            MakeBusinessBaseFactoryMethodsNewObjectVB(ObjectName, vbBBfactoryMethods);

            MakeBusinessBaseFactoryMethodsGetObjectVB(ObjectName, vbBBfactoryMethods);

            MakeBusinessBaseFactoryMethodsDeleteObjectVB(ObjectName, vbBBfactoryMethods);

            MakeBusinessBaseFactoryMethodsSaveObjectVB(ObjectName, vbBBfactoryMethods);

            MakeBusinessBaseFactoryMethodsPrivateCtorVB(vbBBfactoryMethods);



            vbBBfactoryMethods.AppendLine("#End Region");
            return vbBBfactoryMethods.ToString();
        }

        private static void MakeBusinessBaseFactoryMethodsNewObjectVB(string ObjectName, StringBuilder vbBBfactoryMethods)
        {
            vbBBfactoryMethods.AppendFormat("Public Shared Function New{0}() as {0}", ObjectName);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("If Not CanAddObject() Then");
            vbBBfactoryMethods.AppendFormat("Throw New System.Security.SecurityException(\"User not allowed to add a {0}\")", ObjectName);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("End If");
            vbBBfactoryMethods.AppendFormat("Return DataPortal.Create(Of {0})()", ObjectName);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("End Function");
            vbBBfactoryMethods.AppendLine();
        }
        private void MakeBusinessBaseFactoryMethodsGetObjectVB(string ObjectName, StringBuilder vbBBfactoryMethods)
        {
            vbBBfactoryMethods.AppendFormat("Public Shared Function Get{0}(ByVal {1} as {2}) as {0}", ObjectName, fname, ConvertCtoVBTypes(ftype));
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("If Not CanGetObject() Then");
            vbBBfactoryMethods.AppendFormat("Throw New System.Security.SecurityException(\"User not allowed to view a {0}\")", ObjectName);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("End If");
            vbBBfactoryMethods.AppendFormat("Return DataPortal.Fetch(Of {0})(New Criteria({1}))", ObjectName, fname);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("End Function");
            vbBBfactoryMethods.AppendLine();
        }
        private void MakeBusinessBaseFactoryMethodsDeleteObjectVB(string ObjectName, StringBuilder vbBBfactoryMethods)
        {
            vbBBfactoryMethods.AppendFormat("Public Shared Sub Delete{0}(ByVal {1} as {2})", ObjectName, fname, ConvertCtoVBTypes(ftype));
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("If Not CanDeleteObject() Then");
            vbBBfactoryMethods.AppendFormat("Throw New System.Security.SecurityException(\"User not allowed to delete a {0}\")", ObjectName);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("End If");
            vbBBfactoryMethods.AppendFormat("DataPortal.Delete(New Criteria({0}))", fname);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("End Function");
            vbBBfactoryMethods.AppendLine();
        }
        private static void MakeBusinessBaseFactoryMethodsSaveObjectVB(string ObjectName, StringBuilder vbBBfactoryMethods)
        {
            vbBBfactoryMethods.AppendFormat("Public Overrides Function Save() as {0}", ObjectName);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("If IsDeleted AndAlso Not CanDeleteObject() Then");
            vbBBfactoryMethods.AppendFormat("Throw New System.Security.SecurityException(\"User not allowed to delete a {0}\")", ObjectName);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("ElseIf IsNew AndAlso Not CanAddObject() Then");
            vbBBfactoryMethods.AppendFormat("Throw New System.Security.SecurityException(\"User not allowed to add a {0}\")", ObjectName);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("ElseIf Not CanEditObject() Then");
            vbBBfactoryMethods.AppendFormat("Throw New System.Security.SecurityException(\"User not allowed to edit a {0}\")", ObjectName);
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("End If");
            vbBBfactoryMethods.AppendLine("Return MyBase.Save");
            vbBBfactoryMethods.AppendLine();
            vbBBfactoryMethods.AppendLine("End Function");
            vbBBfactoryMethods.AppendLine();
        }
        private static void MakeBusinessBaseFactoryMethodsPrivateCtorVB(StringBuilder vbBBfactoryMethods)
        {
            vbBBfactoryMethods.AppendLine("Private Sub New()");
            vbBBfactoryMethods.AppendLine("'require use of factory methods");
            vbBBfactoryMethods.AppendLine("End Sub");
            vbBBfactoryMethods.AppendLine();
        }

        #endregion

        #region Data Access


        public string MakeBBDataAccessVB(Dictionary<string, string> bbProperties, string DataConnectionName, string BBDataAccessGetSingleStoredProc, string BBAddObjectStoredProc, string BBUpdateStoredProc, string DeleteObjectStoredProc)
        {
            StringBuilder vbBBDataAccess = new StringBuilder();
            vbBBDataAccess.AppendLine("#Region \"Data Access\"");
            vbBBDataAccess.AppendLine();

            MakeBusinessBaseDataAccessCriteriaClassVB(vbBBDataAccess);

            MakeBusinessBaseDataAccessDataPortalCreateVB(bbProperties, vbBBDataAccess);

            MakeBusinessBaseDataAccessDataPortalFetchVB(bbProperties, DataConnectionName, BBDataAccessGetSingleStoredProc, vbBBDataAccess);

            MakeBusinessBaseDataAccessDataPortalInsertVB(DataConnectionName, BBAddObjectStoredProc, vbBBDataAccess);

            MakeBusinessBaseDataAccessDataPortalUpdateVB(DataConnectionName, vbBBDataAccess);

            MakeBusinessBaseDataAccessDoInsertUpdateVB(bbProperties, vbBBDataAccess);

            MakeBusinessBaseDataAccessDeleteSelfVB(vbBBDataAccess);

            MakeBusinessBaseDataAccessDataPortalDeleteVB(DataConnectionName, DeleteObjectStoredProc, vbBBDataAccess);



            vbBBDataAccess.AppendLine("#End Region");
            return vbBBDataAccess.ToString();
        }

        private void MakeBusinessBaseDataAccessCriteriaClassVB(StringBuilder vbBBDataAccess)
        {
            //Criteria Class
            vbBBDataAccess.AppendLine("<Serializable()> _");
            vbBBDataAccess.AppendLine("Private Class Criteria");
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendFormat("Private m{0} as {1}", fname, ConvertCtoVBTypes(ftype));
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendFormat("Public ReadOnly Property {0}() as {1}", TurnFirstToUpper(fname), ConvertCtoVBTypes(ftype));
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("Get");
            vbBBDataAccess.AppendFormat("Return m{0}", TurnFirstToUpper(fname));
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("End Get");
            vbBBDataAccess.AppendLine("End Property");
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendFormat("Public Sub New(ByVal {0} as {1})", fname, ConvertCtoVBTypes(ftype));
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendFormat("m{0} = {1}", TurnFirstToUpper(fname), fname);
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("End Sub");
            vbBBDataAccess.AppendLine("End Class");
            vbBBDataAccess.AppendLine();
        }


        private void MakeBusinessBaseDataAccessDataPortalCreateVB(Dictionary<string, string> bbProperties, StringBuilder vbBBDataAccess)
        {
            //DataPortal Create
            vbBBDataAccess.AppendLine("<RunLocal()> _");
            vbBBDataAccess.AppendLine("Protected Overrides Sub DataPortal_Create()");
            int count = 0;
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                if (count == 0)
                {
                    vbBBDataAccess.AppendFormat("m{0} =", TurnFirstToUpper(kvp.Key));
                }
                else
                {
                    vbBBDataAccess.AppendFormat("{0} =", TurnFirstToUpper(kvp.Key));
                }

                if (kvp.Value == "Guid")
                {
                    vbBBDataAccess.AppendLine("Guid.NewGuid");
                }
                if (kvp.Value == "int")
                {
                    vbBBDataAccess.AppendLine("0");
                }
                if (kvp.Value == "string")
                {
                    vbBBDataAccess.AppendLine("\"\"");
                }
                if (kvp.Value == "SmartDate")
                {
                    vbBBDataAccess.AppendLine("CStr(Today)");
                }
                count = count + 1;
            }
            vbBBDataAccess.AppendLine("ValidationRules.CheckRules");
            vbBBDataAccess.AppendLine("End Sub");
            vbBBDataAccess.AppendLine();
        }


        private void MakeBusinessBaseDataAccessDataPortalFetchVB(Dictionary<string, string> bbProperties, string DataConnectionName, string BBDataAccessGetSingleStoredProc, StringBuilder vbBBDataAccess)
        {
            //DataPortal_Fetch
            vbBBDataAccess.AppendLine("Private Overloads Sub DataPortal_Fetch(ByVal criteria as Criteria)");
            vbBBDataAccess.AppendFormat("Using cn as New SqlConnection(Database.{0})", DataConnectionName);
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("cn.Open()");
            vbBBDataAccess.AppendLine("Using cm as SqlCommand = cn.CreateCommand");
            vbBBDataAccess.AppendLine("cm.CommandType = CommandType.StoredProcedure");
            vbBBDataAccess.AppendFormat("cm.CommandText = \"{0}\"", BBDataAccessGetSingleStoredProc);
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendFormat("cm.Parameters.AddWithValue(\"@{0}\", criteria.{1})", fname, TurnFirstToUpper(fname));
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("Using dr as New SafeDataReader(cm.ExecuteReader)");
            vbBBDataAccess.AppendLine("dr.Read()");
            vbBBDataAccess.AppendLine("With dr");
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                vbBBDataAccess.AppendFormat("m{0} =", TurnFirstToUpper(kvp.Key));
                if (kvp.Value == "string")
                {
                    vbBBDataAccess.AppendFormat(".GetString(\"{0}\")", kvp.Key);
                }
                if (kvp.Value == "Guid")
                {
                    vbBBDataAccess.AppendFormat(".GetGuid(\"{0}\")", kvp.Key);
                }
                if (kvp.Value == "int")
                {
                    vbBBDataAccess.AppendFormat(".GetInt32(\"{0}\")", kvp.Key);
                }
                if (kvp.Value == "SmartDate")
                {
                    vbBBDataAccess.AppendFormat(".GetSmartDate(\"{0}\", m{1}.EmptyIsMin)", kvp.Key, TurnFirstToUpper(kvp.Key));
                }
                vbBBDataAccess.AppendLine();
            }

            vbBBDataAccess.AppendLine(".GetBytes(\"LastChanged\", 0, mTimestamp, 0, 8)");
            vbBBDataAccess.AppendLine("'used with child objects");
            vbBBDataAccess.AppendLine("'.NextResult()");
            vbBBDataAccess.AppendLine("'mResources = ProjectResources.GetProjectResources(dr)");
            vbBBDataAccess.AppendLine("End With");
            vbBBDataAccess.AppendLine("End Using");
            vbBBDataAccess.AppendLine("End Using");
            vbBBDataAccess.AppendLine("End Using");
            vbBBDataAccess.AppendLine("End Sub");
            vbBBDataAccess.AppendLine();
        }


        private static void MakeBusinessBaseDataAccessDataPortalUpdateVB(string DataConnectionName, StringBuilder vbBBDataAccess)
        {
            //DataPortal_Update
            vbBBDataAccess.AppendLine("<Transactional(TransactionalTypes.TransactionScope)> _");
            vbBBDataAccess.AppendLine("Protected Overrides Sub DataPortal_Update()");
            vbBBDataAccess.AppendLine("If MyBase.IsDirty Then");
            vbBBDataAccess.AppendFormat("Using cn As New SqlConnection(Database.{0})", DataConnectionName);
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("cn.Open()");
            vbBBDataAccess.AppendLine("Using cm As SqlCommand = cn.CreateCommand");
            vbBBDataAccess.AppendLine("cm.Parameters.AddWithValue(\"@lastChanged\", mTimestamp)");
            vbBBDataAccess.AppendLine("DoInsertUpdate(cm)");
            vbBBDataAccess.AppendLine("End Using");
            vbBBDataAccess.AppendLine("End Using");
            vbBBDataAccess.AppendLine("End If");
            vbBBDataAccess.AppendLine("'update child object");
            vbBBDataAccess.AppendLine("'mResources.Update(me)");
            vbBBDataAccess.AppendLine("End Sub");
            vbBBDataAccess.AppendLine();
        }
        private static void MakeBusinessBaseDataAccessDataPortalInsertVB(string DataConnectionName, string BBAddObjectStoredProc, StringBuilder vbBBDataAccess)
        {
            //DataPortal_Insert
            vbBBDataAccess.AppendLine("<Transactional(TransactionalTypes.TransactionScope)> _");
            vbBBDataAccess.AppendLine("Protected Overrides Sub DataPortal_Insert()");
            vbBBDataAccess.AppendFormat("Using cn As New SqlConnection(Database.{0})", DataConnectionName);
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("cn.Open()");
            vbBBDataAccess.AppendLine("Using cm As SqlCommand = cn.CreateCommand");
            vbBBDataAccess.AppendFormat("cm.CommandText = \"{0}\"", BBAddObjectStoredProc);
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("DoInsertUpdate(cm)");
            vbBBDataAccess.AppendLine("End Using");
            vbBBDataAccess.AppendLine("End Using");
            vbBBDataAccess.AppendLine("'update child objects");
            vbBBDataAccess.AppendLine("'mResources.Update(Me)");
            vbBBDataAccess.AppendLine("End Sub");
            vbBBDataAccess.AppendLine();
        }

        private void MakeBusinessBaseDataAccessDoInsertUpdateVB(Dictionary<string, string> bbProperties, StringBuilder vbBBDataAccess)
        {
            //DoInsertUpdate
            vbBBDataAccess.AppendLine("Private Sub DoInsertUpdate(ByVal cm as SqlCommand)");
            vbBBDataAccess.AppendLine("With cm");
            vbBBDataAccess.AppendLine(".CommandType = CommandType.StoredProcedure");
            foreach (KeyValuePair<string, string> kvp in bbProperties)
            {
                if (ftype != "SmartDate")
                {
                    vbBBDataAccess.AppendFormat(".Parameters.AddWithValue(\"@{0}\", m{1})", fname, TurnFirstToUpper(fname));
                }
                else
                {
                    vbBBDataAccess.AppendFormat(".Parameters.AddWithValue(\"@{0}\", m{1}.DBValue)", fname, TurnFirstToUpper(fname));
                }
                vbBBDataAccess.AppendLine();
            }
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("Dim param as new SqlParameter(\"@newLastChanged\", SqlDbType.Timestamp)");
            vbBBDataAccess.AppendLine("param.Direction = ParameterDirection.Output");
            vbBBDataAccess.AppendLine(".Parameters.Add(param)");
            vbBBDataAccess.AppendLine(".ExecuteNonQuery()");
            vbBBDataAccess.AppendLine("mTimestamp = CType(.Parameters(\"@newLastChanged\").Value, Byte())");
            vbBBDataAccess.AppendLine("End With");
            vbBBDataAccess.AppendLine("End Sub");
            vbBBDataAccess.AppendLine();
        }
        private void MakeBusinessBaseDataAccessDeleteSelfVB(StringBuilder vbBBDataAccess)
        {
            //DeleteSelf
            vbBBDataAccess.AppendLine("<Transactional(TransactionalTypes.TransactionScope)> _");
            vbBBDataAccess.AppendLine("Protected Overrides Sub DataPortal_DeleteSelf()");
            vbBBDataAccess.AppendFormat("DataPortal_Delete(new Criteria(m{0}))", TurnFirstToUpper(fname));
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("End Sub");
            vbBBDataAccess.AppendLine();
        }
        private void MakeBusinessBaseDataAccessDataPortalDeleteVB(string DataConnectionName, string DeleteObjectStoredProc, StringBuilder vbBBDataAccess)
        {
            //Delete
            vbBBDataAccess.AppendLine("<Transactional(TransactionalTypes.TransactionScope)> _");
            vbBBDataAccess.AppendLine("Private OverLoads Sub DataPortal_Delete(ByVal criteria as Criteria)");
            vbBBDataAccess.AppendFormat("Using cn As New SqlConnection(Database.{0})", DataConnectionName);
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine("cn.Open()");
            vbBBDataAccess.AppendLine("using cm as SqlCommand = cn.CreateCommand");
            vbBBDataAccess.AppendLine("With cm");
            vbBBDataAccess.AppendLine(".Connection= cn");
            vbBBDataAccess.AppendLine(".CommandType = CommandType.StoredProcedure");
            vbBBDataAccess.AppendFormat(".CommandText = \"{0}\"", DeleteObjectStoredProc);
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendFormat(".Parameters.AddWithValue(\"@{0}\", criteria.{1})", fname, TurnFirstToUpper(fname));
            vbBBDataAccess.AppendLine();
            vbBBDataAccess.AppendLine(".ExecuteNonQuery()");
            vbBBDataAccess.AppendLine("End With");
            vbBBDataAccess.AppendLine("End Using");
            vbBBDataAccess.AppendLine("End Using");
            vbBBDataAccess.AppendLine("End Sub");
        }

        #endregion

        #region Exists

        public string MakeBBExistsVB(string ObjectName, string DataConnectionName, string ObjectExistsStoredProc)
        {
            StringBuilder vbbbExists = new StringBuilder();
            vbbbExists.AppendLine("#Region \"Exists\"");
            vbbbExists.AppendLine();

            MakeBusinessBaseBExistsBoolVB(vbbbExists);

            MakeBusinessBaseBExistsCommandVB(vbbbExists);

            MakeBusinessBaseBExistsBoolObjectExistVB(ObjectName, vbbbExists);
            MakeBusinessBaseBExistsBoolVB(ObjectName, vbbbExists);

            vbbbExists.AppendFormat("Private Sub New(ByVal {0} As {1})", fname, ConvertCtoVBTypes(ftype));
            vbbbExists.AppendLine();
            vbbbExists.AppendFormat("m{0} = {1}", TurnFirstToUpper(fname), fname);
            vbbbExists.AppendLine();
            vbbbExists.AppendLine("End Sub");
            vbbbExists.AppendLine();

            MakeBusinessBaseBExistsDataPortalExecuteVB(DataConnectionName, vbbbExists);

            vbbbExists.AppendLine("End Class");


            vbbbExists.AppendLine("#End Region");
            return vbbbExists.ToString();
        }

        private void MakeBusinessBaseBExistsBoolVB(StringBuilder vbbbExists)
        {
            //Exists
            vbbbExists.AppendFormat("Public Shared Function Exists(ByVal {0} As {1}) As Boolean", fname, ConvertCtoVBTypes(ftype));
            vbbbExists.AppendLine();
            vbbbExists.AppendFormat("Return ExistsCommand.Exists({0})", fname);
            vbbbExists.AppendLine();
            vbbbExists.AppendLine("End Function");
            vbbbExists.AppendLine();
        }
        private static void MakeBusinessBaseBExistsBoolObjectExistVB(string ObjectName, StringBuilder vbbbExists)
        {
            vbbbExists.AppendFormat("Public ReadOnly Property {0}Exists() As Boolean", ObjectName);
            vbbbExists.AppendLine();
            vbbbExists.AppendLine("Get");
            vbbbExists.AppendLine("Return mExists");
            vbbbExists.AppendLine("End Get");
            vbbbExists.AppendLine("End Property");
            vbbbExists.AppendLine();
        }
        private void MakeBusinessBaseBExistsCommandVB(StringBuilder vbbbExists)
        {
            //ExistsCommand Class
            vbbbExists.AppendLine("<Serializable()> _");
            vbbbExists.AppendLine("Private Class ExistsCommand");
            vbbbExists.AppendLine("Inherits CommandBase");
            vbbbExists.AppendLine();
            vbbbExists.AppendFormat("Private m{0} As {1}", fname, ConvertCtoVBTypes(ftype));
            vbbbExists.AppendLine();
            vbbbExists.AppendFormat("Private mExists As Boolean");
            vbbbExists.AppendLine();
        }
        private void MakeBusinessBaseBExistsDataPortalExecuteVB(string DataConnectionName, StringBuilder vbbbExists)
        {
            vbbbExists.AppendLine("Protected Overrides Sub DataPortal_Execute()");
            vbbbExists.AppendFormat("Using cn As New SqlConnection(Database.{0})", DataConnectionName);
            vbbbExists.AppendLine();
            vbbbExists.AppendLine("cn.Open()");
            vbbbExists.AppendLine("Using cm As SqlCommand = cn.CreateCommand");
            vbbbExists.AppendLine("cm.CommandType = CommandType.Text");
            vbbbExists.AppendFormat("cm.Parameters.AddWithValue(\"@{0}\", m{1})", fname, TurnFirstToUpper(fname));
            vbbbExists.AppendLine();
            vbbbExists.AppendLine("Dim count As Integer = CInt(cm.ExecuteScalar)");
            vbbbExists.AppendLine("mExists = (count >0)");
            vbbbExists.AppendLine("End Using");
            vbbbExists.AppendLine("End Using");
            vbbbExists.AppendLine("End Sub");
            vbbbExists.AppendLine();
        }
        private void MakeBusinessBaseBExistsBoolVB(string ObjectName, StringBuilder vbbbExists)
        {
            vbbbExists.AppendFormat("Public Shared Function Exists(ByVal {0} As {1}) As Boolean", fname, ConvertCtoVBTypes(ftype));
            vbbbExists.AppendLine();
            vbbbExists.AppendLine("Dim result As ExistsCommand");
            vbbbExists.AppendFormat("result = DataPortal.Execute(Of ExistsCommand)(New ExistsCommand({0}))", fname);
            vbbbExists.AppendLine();
            vbbbExists.AppendFormat("Return result.{0}Exists", ObjectName);
            vbbbExists.AppendLine();
            vbbbExists.AppendLine("End Function");
            vbbbExists.AppendLine();
        }

        #endregion


        #endregion

        #region ReadOnly

        public string MakeReadOnlyBusinessBaseVB(Dictionary<string, string> roBusinessBaseProperties)
        {
            StringBuilder vbroBBBusiness = new StringBuilder();
            vbroBBBusiness.AppendLine("#Region \"Business Methods\"");
            vbroBBBusiness.AppendLine();

            //declarations
            int count = 0;
            foreach (KeyValuePair<string, string> kvp in roBusinessBaseProperties)
            {
                if (count == 0)
                {
                    fname = kvp.Key.ToString();
                    ftype = kvp.Value.ToString();
                }
                vbroBBBusiness.AppendFormat("Private m{0} As {1}", TurnFirstToUpper(kvp.Key), ConvertCtoVBTypes(kvp.Value));
                vbroBBBusiness.AppendLine();
            }
            vbroBBBusiness.AppendLine();

            //getset
            foreach (KeyValuePair<string, string> kvp in roBusinessBaseProperties)
            {
                vbroBBBusiness.AppendFormat("Public Property {0}() As {1}", TurnFirstToUpper(kvp.Key), ConvertCtoVBTypes(kvp.Value));
                vbroBBBusiness.AppendLine();
                vbroBBBusiness.AppendLine("Get");
                vbroBBBusiness.AppendFormat("Return m{0}", TurnFirstToUpper(kvp.Key));
                vbroBBBusiness.AppendLine();
                vbroBBBusiness.AppendLine("End Get");
                vbroBBBusiness.AppendFormat("Friend Set(ByVal value As {0})",ConvertCtoVBTypes(kvp.Value));
                vbroBBBusiness.AppendLine();
                vbroBBBusiness.AppendFormat("m{0}=value",TurnFirstToUpper(kvp.Key));
                vbroBBBusiness.AppendLine();
                vbroBBBusiness.AppendLine("End Set");
                vbroBBBusiness.AppendLine("End Property");
                vbroBBBusiness.AppendLine();
            }

            vbroBBBusiness.AppendLine("Protected Overrides Function GetIdValue() As Object");
            vbroBBBusiness.AppendFormat("Return m{0}",TurnFirstToUpper(fname));
            vbroBBBusiness.AppendLine();
            vbroBBBusiness.AppendLine("End Function");
            vbroBBBusiness.AppendLine();

            vbroBBBusiness.AppendLine("Public Overrides Function ToString() As String");
            vbroBBBusiness.AppendFormat("Return m{0}.ToString", TurnFirstToUpper(fname));
            vbroBBBusiness.AppendLine();
            vbroBBBusiness.AppendLine("End Function");
            vbroBBBusiness.AppendLine();
  

            vbroBBBusiness.AppendLine("#End Region");
            return vbroBBBusiness.ToString();
        }

        public string MakeReadOnlyConstructorsVB(string ObjectName, Dictionary<string, string> roBusinessBaseProperties)
        {
            StringBuilder vbroBBctors = new StringBuilder();
            vbroBBctors.AppendLine("#Region \"Constructors\"");
            vbroBBctors.AppendLine();

            vbroBBctors.AppendLine("Private Sub New()");
            vbroBBctors.AppendLine("' require use of factory methods");
            vbroBBctors.AppendLine("End Sub");
            vbroBBctors.AppendLine();

            vbroBBctors.Append("Private Sub New(");
            foreach (KeyValuePair<string, string> kvp in roBusinessBaseProperties)
            {
                vbroBBctors.AppendFormat("ByVal {0} As {1},", kvp.Key, ConvertCtoVBTypes(kvp.Value));
            }
            vbroBBctors.Remove((vbroBBctors.Length - 1), 1);
            vbroBBctors.AppendLine(")");
            foreach (KeyValuePair<string, string> kvp in roBusinessBaseProperties)
            {
                vbroBBctors.AppendFormat("m{0}={1}", TurnFirstToUpper(kvp.Key), kvp.Key);
                vbroBBctors.AppendLine();
            }
            vbroBBctors.AppendLine();
            vbroBBctors.AppendLine("End Sub");

            vbroBBctors.AppendLine("#End Region");
            return vbroBBctors.ToString();
        }

        
        #endregion

        #region ReadOnly List
        #region FactoryMethods


        public string MakeReadOnlyListBaseFactoryMethodsVB(string ObjectName, string FilteredCriteriaType, string FilteredCriteriaName)
        {
            StringBuilder rolbVBFactoryMethods = new StringBuilder();
            rolbVBFactoryMethods.AppendLine("#Region \"Factory Methods\"");
            rolbVBFactoryMethods.AppendLine();

            MakeReadOnlyListBaseForceFactoryMethodVB(rolbVBFactoryMethods);

            MakeReaonOnlyListBaseFactoryMethodsGetAllVB(ObjectName, rolbVBFactoryMethods);

            MakeReadOnlyListBaseFactoryMethodsGetFilteredVB(ObjectName, FilteredCriteriaType, FilteredCriteriaName, rolbVBFactoryMethods);



            rolbVBFactoryMethods.AppendLine("#End Region");
            return rolbVBFactoryMethods.ToString();
        }

        private static void MakeReaonOnlyListBaseFactoryMethodsGetAllVB(string ObjectName, StringBuilder rolbVBFactoryMethods)
        {
            //Get All
            rolbVBFactoryMethods.AppendFormat("Public Shared Function Get{0}() As {0}List", ObjectName);
            rolbVBFactoryMethods.AppendLine();
            rolbVBFactoryMethods.AppendFormat("Return DataPortal.Fetch(Of {0}List)(New Criteria)", ObjectName);
            rolbVBFactoryMethods.AppendLine();
            rolbVBFactoryMethods.AppendLine("End Function");
            rolbVBFactoryMethods.AppendLine();
        }
        private void MakeReadOnlyListBaseFactoryMethodsGetFilteredVB(string ObjectName, string FilteredCriteriaType, string FilteredCriteriaName, StringBuilder rolbVBFactoryMethods)
        {
            //Get FilteredCriteriaName
            rolbVBFactoryMethods.AppendFormat("Public Shared Function Get{0}(ByVal {1} As {2}) As {0}List", ObjectName, FilteredCriteriaName, ConvertCtoVBTypes(FilteredCriteriaType));
            rolbVBFactoryMethods.AppendLine();
            rolbVBFactoryMethods.AppendFormat("Return DataPortal.Fetch(Of {0}List)(New FilteredCriteria({1}))", ObjectName, FilteredCriteriaName);
            rolbVBFactoryMethods.AppendLine();
            rolbVBFactoryMethods.AppendLine("End Function");
            rolbVBFactoryMethods.AppendLine();
        }
        private static void MakeReadOnlyListBaseForceFactoryMethodVB(StringBuilder rolbVBFactoryMethods)
        {
            //force factory use
            rolbVBFactoryMethods.AppendLine("Private Sub New()");
            rolbVBFactoryMethods.AppendLine("' require use of factory methods");
            rolbVBFactoryMethods.AppendLine("End Sub");
            rolbVBFactoryMethods.AppendLine();
        }
        #endregion



        public string MakeReadOnlyListBaseDataAccessVB(string ObjectName, int FilteredCriteriaTypeIndex, string FilteredCriteriaType, string FilteredCriteriaName, string DataConnectionName, string ROLBSP)
        {
            StringBuilder rolbvbListBaseDataAccess = new StringBuilder();
            rolbvbListBaseDataAccess.AppendLine("#Region \"Data Access\"");
            rolbvbListBaseDataAccess.AppendLine();

            //CriteriaClass
            rolbvbListBaseDataAccess.AppendLine("<Serializable()> _");
            rolbvbListBaseDataAccess.AppendLine("Private Class Criteria");
            rolbvbListBaseDataAccess.AppendLine("'no criteria - retrieve all");
            rolbvbListBaseDataAccess.AppendLine("End Class");
            rolbvbListBaseDataAccess.AppendLine();

            //FilteredCriteria Class
            rolbvbListBaseDataAccess.AppendLine("<Serializable()> _");
            rolbvbListBaseDataAccess.AppendLine("Private Class FilteredCriteria");
            rolbvbListBaseDataAccess.AppendFormat("Private m{0} As {1}", TurnFirstToUpper(FilteredCriteriaName), ConvertCtoVBTypes(FilteredCriteriaType));
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendFormat("Public ReadOnly Property {0}() As {1}", TurnFirstToUpper(FilteredCriteriaName), ConvertCtoVBTypes(FilteredCriteriaType));
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendLine("Get");
            rolbvbListBaseDataAccess.AppendFormat("Return m{0}", TurnFirstToUpper(FilteredCriteriaName));
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendLine("End Get");
            rolbvbListBaseDataAccess.AppendLine("End Property");
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendFormat("Public Sub New(ByVal {0} as {1})", FilteredCriteriaName, ConvertCtoVBTypes(FilteredCriteriaType));
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendFormat("m{0}={1}", TurnFirstToUpper(FilteredCriteriaName), FilteredCriteriaName);
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendLine("End Sub");
            rolbvbListBaseDataAccess.AppendLine("End Class");
            rolbvbListBaseDataAccess.AppendLine();

            //FetchNoCriteria
            rolbvbListBaseDataAccess.AppendLine("Private Overloads Sub DataPortal_Fetch(ByVal criteria As Criteria)");
            rolbvbListBaseDataAccess.AppendLine("'fetch with no filter");
            rolbvbListBaseDataAccess.AppendLine("Fetch(\"\")");
            rolbvbListBaseDataAccess.AppendLine("End Sub");
            rolbvbListBaseDataAccess.AppendLine();

            //Fetch Filtered Criteria
            rolbvbListBaseDataAccess.AppendLine("Private Overloads Sub DataPortal_Fetch(ByVal criteria as FilteredCriteria)");
            rolbvbListBaseDataAccess.AppendFormat("Fetch(criteria.{0})", TurnFirstToUpper(FilteredCriteriaName));
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendLine("End Sub");
            rolbvbListBaseDataAccess.AppendLine();

            //FetchMethod
            rolbvbListBaseDataAccess.AppendFormat("Private Sub Fetch(ByVal {0}Filter as {1})", FilteredCriteriaName, ConvertCtoVBTypes(FilteredCriteriaType));
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendLine("RaiseListChangedEvents=False");
            rolbvbListBaseDataAccess.AppendFormat("Using cn As New SqlConnection(Database.{0})", DataConnectionName);
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendLine("cn.Open()");
            rolbvbListBaseDataAccess.AppendLine("Using cm As SqlCommand = cn.CreateCommand");
            rolbvbListBaseDataAccess.AppendLine("With cm");
            rolbvbListBaseDataAccess.AppendLine(".CommandType = CommandType.StoredProcedure");
            rolbvbListBaseDataAccess.AppendFormat(".CommandText= \"{0}\"", ROLBSP);
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendLine("Using dr As New SafeDataReader(.ExecuteReader)");
            rolbvbListBaseDataAccess.AppendLine("IsReadOnly = False");
            rolbvbListBaseDataAccess.AppendLine("While dr.Read()");
            rolbvbListBaseDataAccess.AppendFormat("Dim info As New {0}List(dr.GetGuid(0), dr.GetString(1))", ObjectName);
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendFormat("If Len({0}Filter)=0 OrElse info.{1}.IndexOf({0}Filter)=0 Then", FilteredCriteriaName, TurnFirstToUpper(FilteredCriteriaName));
            rolbvbListBaseDataAccess.AppendLine();
            rolbvbListBaseDataAccess.AppendLine("Me.Add(info)");
            rolbvbListBaseDataAccess.AppendLine("End If");
            rolbvbListBaseDataAccess.AppendLine("End While");
            rolbvbListBaseDataAccess.AppendLine("IsReadOnly = True");
            rolbvbListBaseDataAccess.AppendLine("End Using");
            rolbvbListBaseDataAccess.AppendLine("End With");
            rolbvbListBaseDataAccess.AppendLine("End Using");
            rolbvbListBaseDataAccess.AppendLine("End Using");
            rolbvbListBaseDataAccess.AppendLine("RaiseListChangedEvents = True");
            rolbvbListBaseDataAccess.AppendLine("End Sub");



            rolbvbListBaseDataAccess.AppendLine("#End Region");
            return rolbvbListBaseDataAccess.ToString();
        }


        #endregion



        public string MakeCloseClassVB()
        {
            StringBuilder closeVB = new StringBuilder();
            closeVB.AppendLine("End Class");

            return closeVB.ToString();
        }

        #endregion
    }
}
